package com.yanqu.road.server.manger.activity.goddess;

import com.yanqu.road.dao.impl.activity.goddess.GoddessConfigDaoImpl;
import com.yanqu.road.dao.impl.activity.goddess.GoddessUserDataDaoImpl;
import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.cat.Position;
import com.yanqu.road.entity.activity.goddess.GoddessCell;
import com.yanqu.road.entity.activity.goddess.GoddessColorStone;
import com.yanqu.road.entity.activity.goddess.GoddessUserData;
import com.yanqu.road.entity.activity.goddess.GoddessYinYangStone;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.pb.activity.GoddessProto;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.goddess.GoddessConfig;
import com.yanqu.road.server.gameplayer.module.activity.goddess.GoddessModule;
import com.yanqu.road.server.gameplayer.module.activity.goddess.GoddessPb;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.utils.string.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class GoddessMgr extends TempMgr {

    private static GoddessConfig config;

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }

    public static void reloadActivityData() {

        getLogger().info("reload Goddess Activity start");

        List<ActivityInfo> openActivityInfoList = NormalActivityMgr.getOpenActivityInfoList(eActivityType.GoddessActivity.getValue());
        if (openActivityInfoList.isEmpty()) {
            getLogger().info("no Goddess Activity in show time");
            config = null;

        } else {

            ActivityInfo activityInfo = openActivityInfoList.get(0);
            GoddessConfig tempConfig = new GoddessConfig(activityInfo);
            int tmpActivityId = activityInfo.getActivityId();

            GoddessConfigDaoImpl configDao = new GoddessConfigDaoImpl();

            tempConfig.setMissionInfoList(configDao.getGoddessMissionInfoList(tmpActivityId));
            tempConfig.setEraInfoList(configDao.getGoddessEraInfoList(tmpActivityId));
            tempConfig.setLiangYiStoneInfoList(configDao.getGoddessLiangYiStoneInfoList(tmpActivityId));
            tempConfig.setShapeInfoList(configDao.getGoddessRandomStoneInfoList(tmpActivityId));
            tempConfig.setPeopleInfoList(configDao.getGoddessPeopleInfoList(tmpActivityId));
            tempConfig.setBookInfoList(configDao.getGoddessPeoBookInfoList(tmpActivityId));

            Map<String, ActivityConfig> activityConfigMap = NormalActivityMgr.getActivityConfigMap(activityInfo.getActivityId());
            if(activityConfigMap != null){
                tempConfig.init(activityConfigMap);
            }

            config = tempConfig;
        }

        for (GamePlayer player : GamePlayerMgr.getAllOnlinePlayer()) {
            player.getModule(GoddessModule.class).activityReload();
        }

        getLogger().info("reload Goddess Activity finish");
    }


    /**
     * 查看玩家补天数据
     */
    public static GoddessProto.GoddessOtherUserSimpleDataRespMsg.Builder watch(int activityId, long userId){
        GoddessProto.GoddessOtherUserSimpleDataRespMsg.Builder builder = GoddessProto.GoddessOtherUserSimpleDataRespMsg.newBuilder();
        GamePlayer player = GamePlayerMgr.getOnlinePlayer(userId);
        GoddessUserData userData;
        if(player != null){
            userData = player.getModule(GoddessModule.class).getUserData();
        }else {
            userData = new GoddessUserDataDaoImpl().getGoddessUserData(activityId, userId);
        }
        if(userData != null){
            for (GoddessCell goddessCell : userData.getRankCellList()) {
                GoddessProto.GoddessCellDataTemp.Builder cellBuilder = GoddessPb.buildCellData(goddessCell);
                builder.addCell(cellBuilder);
            }
            for (Map.Entry<Integer, GoddessColorStone> stoneEntry : userData.getRankColorStoneMap().entrySet()) {
                GoddessColorStone stone = stoneEntry.getValue();
                if(stone.getCell() > -1){
                    builder.addColorStone(GoddessPb.buildColorStoneData(stone));
                }
            }
            for (int i = 0; i < userData.getRankYinStoneList().size(); i++) {
                GoddessYinYangStone stone = userData.getYinStoneList().get(i);
                if(stone.getPos() > -1){
                    builder.addYinStone(GoddessPb.buildYiStoneData(stone));
                }
            }
            for (int i = 0; i < userData.getRankYangStoneList().size(); i++) {
                GoddessYinYangStone stone = userData.getYangStoneList().get(i);
                if(stone.getPos() > -1){
                    builder.addYangStone(GoddessPb.buildYiStoneData(stone));
                }
            }
            builder.setColorStonePoint(userData.getRankCellPoint());
            builder.setMissionId(userData.getYearStatus().getMissionId());
            builder.setEraId(userData.getRankEra());
        }
        builder.setRet(0);
        return builder;
    }

    public static GoddessCell copyCell(GoddessCell cell){
        GoddessCell temp = new GoddessCell();
        temp.setOpen(cell.getOpen());
        temp.setStoneId(cell.getStoneId());
        temp.setInfectId(cell.getInfectId());
        temp.setPos(cell.getPos());
        return temp;
    }

    public static GoddessColorStone copyGoddessColorStone(GoddessColorStone stone){
        GoddessColorStone temp = new GoddessColorStone();
        temp.setId(stone.getId());
        temp.setRe(stone.getRe());
        temp.setSd(stone.getSd());
        temp.setPt(stone.getPt());
        temp.setCell(stone.getCell());
        temp.setFs(stone.getFs());
        return temp;
    }

    public static GoddessYinYangStone copyGoddessYinYangStone(GoddessYinYangStone yinYangStone){
        GoddessYinYangStone temp = new GoddessYinYangStone();
        temp.setId(yinYangStone.getId());
        temp.setPos(yinYangStone.getPos());
        temp.setType(yinYangStone.getType());
        temp.setLv(yinYangStone.getLv());
        return temp;
    }

    public static int getIndex(Position position, GoddessConfig config) {
        return position.getY() * config.COLUMN_NUM + position.getX();
    }

    /**
     * 元素下标转成坐标位置
     */
    public static Position coordinate(int index, GoddessConfig config){
        int y = index / config.COLUMN_NUM;
        int x = index - y * config.COLUMN_NUM;
        return new Position(x, y);
    }

    /**
     * 计算嵌入的所有位置
     * @param shape  0;1;0|1;1;1|0;1;0
     */
    public static List<Integer> getInLayPositionList(int pos, String shape, GoddessConfig config) {
        //GoddessMgr.getLogger().info("=========== shape {}", shape);
        Position position = GoddessMgr.coordinate(pos, config);
        int firstX = -1;
        int firstY = -1;
        List<Integer> result = new ArrayList<>();
        result.add(pos);
        //getLogger().info("======= pos {}", pos);
        List<String> rowList = StringUtils.stringToStringList(shape, "\\|");
        //getLogger().info("========");
        //for (int i = rowList.size() - 1; i >= 0; i--) {
            //getLogger().info("======== {}", rowList.get(i));
        //}
        //getLogger().info("========");
        List<List<Integer>> rowColList = new ArrayList<>();
        for (int r = 0; r < rowList.size(); r++) {
            List<Integer> temp = new ArrayList<>();
            String row = rowList.get(r);
            List<Integer> colList = StringUtils.stringToIntegerList(row, ";");
            for (int c = 0; c < colList.size(); c++) {
                int val = colList.get(c);
                if(firstX == -1){
                    if(val == 1){
                        firstX = c;
                        firstY = r;
                    }
                    temp.add(pos);
                    continue;
                }
                if(val == 1){
                    int x = position.getX() + c - firstX;
                    int y = position.getY() + r - firstY;
                    if(x < 0 || x >= config.COLUMN_NUM || y < 0 || y >= config.ROW_NUM){
                        return new ArrayList<>();
                    }
                    int index = GoddessMgr.getIndex(new Position(x, y), config);
                    result.add(index);
                    temp.add(index);
                    continue;
                }
                temp.add(val);
            }
            rowColList.add(temp);
        }
//        getLogger().info("========");
//        for (int i = rowColList.size() - 1; i >= 0; i--) {
//            List<Integer> temp = rowColList.get(i);
//            getLogger().info("======== {}", StringUtils.listToString(temp, ";"));
//        }
//        getLogger().info("========");
//        getLogger().info("======== {}", StringUtils.listToString(result, ","));
        return result;
    }

    public static GoddessConfig getConfig() {
        return config;
    }

    public static int getActivityId(){
        if(config == null){
            return 0;
        }
        return config.getActivityInfo().getActivityId();
    }

    public static ActivityInfo getActivityInfo() {
        if(config == null){
            return null;
        }
        return config.getActivityInfo();
    }
}
